package taboo

import (
	"fmt"
	"sync"
)

type regCache struct {
	sync.RWMutex
	regDict map[string]*MatchReg
}

var reg_cache = &regCache{
	regDict: make(map[string]*MatchReg),
}

func SetCacheReg(name string, reg *MatchReg) error {
	if name == `` {
		return fmt.Errorf(`SetCacheReg must have a valid name. Otherwise use SetDefaultReg`)
	}
	reg_cache.Lock()
	defer reg_cache.Unlock()
	reg_cache.regDict[name] = reg
	return nil
}

func GetCacheReg(name string) (*MatchReg, bool) {
	reg_cache.RLock()
	defer reg_cache.RUnlock()
	tree, ok := reg_cache.regDict[name]
	return tree, ok
}

func SetDefaultReg(tree *MatchReg) {
	reg_cache.Lock()
	defer reg_cache.Unlock()
	reg_cache.regDict[``] = tree
}

func GetDefaultReg() *MatchReg {
	reg_cache.RLock()
	defer reg_cache.RUnlock()
	tree := reg_cache.regDict[``]
	return tree
}
